package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;

import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;


    @Autowired
    private StatementMapper statementMapper;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment oldAppointment = this.get(appointment.getId());
        //当状态不是预约中的时候不能编辑
        if (! Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())){
            throw new BusinessException("当前状态不是预约中不能进行编辑");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);

        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    /**
     * 用户到店
     * @param id 到店的用户的id
     */
    @Override
    public void arralShow(Long id) {
        //先通过id查询出对应的用户
        Appointment appointment = this.get(id);
        //判断该是否是预约中的状态,如果是就把用户的状态设置为到店状态,如果不是就抛出异常
        if (!appointment.getStatus().equals(Appointment.STATUS_APPOINTMENT)) {
            throw new BusinessException("非法操作");
        }
        //                                   设置用户的状态为到店
        appointmentMapper.changeArralShow(id,Appointment.STATUS_ARRIVAL);
    }

    /**
     * 用户取消
     * @param id 需要取消的用户的id
     */
    @Override
    public void cancel(Long id) {
        //先通过id查询出对应的用户
        Appointment appointment = this.get(id);
        //判断是否是预约中的状态,如果是就把状态设置为取消状态,如果不是就抛出异常
        if (!appointment.getStatus().equals(Appointment.STATUS_APPOINTMENT)) {
            throw new BusinessException("非法操作");
        }
        //                                把用户的状态设置为取消状态
        appointmentMapper.changecancel(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public Long generateConsume(Long id) {
        //根据预约单id查询结算单
        Statement statement = statementMapper.queryByAppointmentId(id);
        //如果结算单为null就创建出一个新的结算单
        if (statement == null) {
            statement = new Statement();
            //3根据预约单的id查询出来预约单
            Appointment appointment = this.get(id);
            statement.setCreateTime(new Date());
            statement.setAppointmentId(id);
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setInfo(appointment.getInfo());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setServiceType(appointment.getServiceType());
            statementMapper.insert(statement);
        }
        //修改预约单的状态为已结算的状态
        appointmentMapper.changecancel(id ,Appointment.STATUS_SETTLE);
        return statement.getId();
    }


}
